package com.yhny.lykj.yuhannongye.ui;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.longsichao.lscframe.ctrl.DataManager;
import com.longsichao.lscframe.model.LSCModel;
import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.model.BaseModel;
import com.yhny.lykj.yuhannongye.model.SmsModel;
import com.yhny.lykj.yuhannongye.net.NetHelper;
import com.yhny.lykj.yuhannongye.tools.LCAppUtils;
import com.yhny.lykj.yuhannongye.tools.Tools;

public class RegisterActivity extends BaseActivity {
    private TextView topTv;
    private EditText phoneEt;
    private EditText codeEt;
    private EditText passwordEt;
    private EditText psdAgainEt;
    private Button codeBt;

    //验证码按钮计时器
    private TimeCount time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        TextView textView = (TextView)findViewById(R.id.name_top_bar);
        textView.setText("用户注册");
        findViewById(R.id.left_group).setOnClickListener(this);
        findViewById(R.id.left_text).setOnClickListener(this);
        phoneEt = (EditText)findViewById(R.id.register_phone);
        codeEt = (EditText)findViewById(R.id.register_code);
        passwordEt = (EditText)findViewById(R.id.register_password);
        psdAgainEt = (EditText)findViewById(R.id.register_password_again);
        codeBt = (Button) findViewById(R.id.register_code_bt);
        codeBt.setOnClickListener(this);
        findViewById(R.id.register_submit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.left_group:
            case R.id.left_text:{
                finish();
                break;
            }
            case R.id.register_code_bt:{
                //获取验证码
                //发送短信验证码
                String phone = phoneEt.getText().toString();
                if (phone.isEmpty()){
                    LSCToast.show(this,"请输入手机号");
                    return;
                }
                if (!Tools.isMobilePhoneNumber(phone)){
                    LSCToast.show(this,"请输入正确的手机号");
                    return;
                }
                showProgressDialog(R.string.text_loading);
                DataManager.getData(MODEL_PROCESS_1, NetHelper.User.sendSMS(phone), this);
                break;
            }
            case R.id.register_submit:{
                //注册
                String phone = phoneEt.getText().toString();
                String code = codeEt.getText().toString();
                String password = passwordEt.getText().toString();
                String psdagain = psdAgainEt.getText().toString();
                if (phone.isEmpty()){
                    LSCToast.show(this,"请输入手机号");
                    return;
                }
                if (!Tools.isMobilePhoneNumber(phone)){
                    LSCToast.show(this,"请输入正确的手机号");
                    return;
                }
                if (code.isEmpty()){
                    LSCToast.show(this,"请输入验证码");
                    return;
                }
                if (password.isEmpty()){
                    LSCToast.show(this,"请输入密码");
                    return;
                }
                if (password.length()<6&&password.length()>32){
                    LSCToast.show(this,"请输入6-32位密码");
                    return;
                }
                if (psdagain.isEmpty()){
                    LSCToast.show(this,"请再次输入密码");
                    return;
                }
                if (!password.equals(psdagain)){
                    LSCToast.show(this,"两次密码输入的不一致");
                    return;
                }

                showProgressDialog(R.string.text_loading);
                DataManager.getData(MODEL_PROCESS_0, NetHelper.User.register(phone,password,code, LCAppUtils.getIp(this)), this);
                break;
            }
        }
    }
    @Override
    public boolean onModel(int caseId, LSCModel model) {
        if (super.onModel(caseId, model)) {
            dismissProgressDialog();
            return false;
        }
        switch (caseId){
            case MODEL_PROCESS_0:{
                BaseModel baseModel = (BaseModel)model;
                if (baseModel == null || baseModel.equals("")||baseModel.status==NET_ERROR||baseModel.status!=NET_SUSSEC){
                    LSCToast.show(this,baseModel.msg);
                }else {
                    if (baseModel.status!=1){
                        LSCToast.show(this,baseModel.msg);
                    }
                    LSCToast.show(this,"注册成功！");
                    finish();
                }
                break;
            }
            case MODEL_PROCESS_1:{
                BaseModel baseModel = (BaseModel)model;
                if (baseModel==null||baseModel.status!=1){
                    LSCToast.show(this,"发送失败");
                }else {
                    //进行验证
                    time=new TimeCount(60000,1000);//参数依次为总时长，计时时间间隔
                    time.start();
                    LSCToast.show(this,"发送成功");
                }
                break;
            }
        }
        dismissProgressDialog();
        return false;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (time!=null)
        {
            time.cancel();
            time=null;
        }
    }

    class  TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程显示
        @Override
        public void onTick(long millisUntilFinished) {
            codeBt.setTextSize(14);
            codeBt.setText(millisUntilFinished / 1000 + "秒后重新发送");
            codeBt.setClickable(false);
        }
        //计时完成触发
        @Override
        public void onFinish() {
            codeBt.setText("获取验证码");
            codeBt.setClickable(true);
        }
    }

}
